package oriol.forns.gutierrez;

import java.util.ArrayList;
import java.util.Random;





import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Nuvols {
	int max = 8;
	ArrayList<Objecte> nuvols;
	Resources resources;
	BitmapFactory.Options options;
	private Random rand;
	
	public Nuvols() {
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		nuvols = new ArrayList<Objecte>();
		resources = GV.Instancies.menu.getResources();
		rand = new Random();
	}

	public void actualitza() {
		tocaafegir();
		for(int i = nuvols.size()-1; i >= 0 ; i--) {
			Objecte nuvol = nuvols.get(i);
			nuvol.actualitza();
			if ((nuvol.vx > 0 && nuvol.x > GV.Screen.metrics.widthPixels) || (nuvol.vx < 0 && nuvol.x < -nuvol.tx)){
				nuvols.remove(i);
			}
		}
	}

	private void tocaafegir() {
		if(nuvols.size() == max) return;
		for(int i=nuvols.size(); i<max; i++) {
			Bitmap nuvol;
			if(rand.nextInt(2)==0) nuvol = BitmapFactory.decodeResource(resources, R.drawable.cloud1, options);
			else nuvol = BitmapFactory.decodeResource(resources, R.drawable.cloud2, options);
			float x,y,tx,ty,vx,vy;
			tx = (250f+(float)rand.nextInt(250))/1000*GV.Screen.metrics.widthPixels;
			ty = (125f +(float)rand.nextInt(150))/1000*GV.Screen.metrics.heightPixels;
			y = (float)rand.nextInt(GV.Screen.metrics.heightPixels-(int)ty);
			vx = (0.0015f+(float)rand.nextInt(40)/10000)*GV.Screen.metrics.widthPixels;
			vy = 0;
			if(rand.nextInt(2)==0) {
				x = -tx;
			}
			else {
				x = GV.Screen.metrics.widthPixels;
				vx = -vx;
			}
			nuvols.add(new Objecte(x,y,vx,vy,tx,ty, nuvol));
		}
	}

	public void draw(Canvas canvas) {
		for (int i = 0; i < nuvols.size(); ++i) {
			nuvols.get(i).draw(canvas);
		}
	}
}
